package com.mr.base.dao.hibernate3;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.mr.base.constant.MrBaseConstant;
import com.mr.base.dao.CardDao;
import com.mr.base.domain.Card;
import com.skysz.framework.dao.hibernate3.AbstractDomainDao;

public class CardDaoImpl extends AbstractDomainDao<Card> implements CardDao {
	private static final long serialVersionUID = 1L;

	@Override
	public Class<Card> getDomainClazz() {
		return Card.class;
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void deleteBcard(Long id) {
		Session session = this.getSession();
		String hql = "delete from Bcard t where t.id=?";
		String hql2 = "delete from BcardLine t where t.card.id=?";
		
		Query query = session.createQuery(hql);
		query.setLong(0, id);
		query.executeUpdate();
		
		query = session.createQuery(hql2);
		query.setLong(0, id);
		query.executeUpdate();
		session.flush();
	}
	
	/**
	 * 删除行
	 * @param id
	 */
	public void deleteBcardLine(Long id) {
		Session session = this.getSession();
		String hql = "delete from BcardLine t where t.card.id=?";
		
		Query query = session.createQuery(hql);
		query.setLong(0, id);
		query.executeUpdate();
		session.flush();
	}
	
	/**
	 * 禁用
	 * @param id
	 */
	public void disableBcard(Long id) {
		String hql = "update Bcard t set t.enable=false where t.id=?";
		Session session = this.getSession();
		Query query = session.createQuery(hql);
		query.setLong(0, id);
		query.executeUpdate();
		session.flush();
	}
	
	/**
	 * 加载卡信息
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Card loadBcard(Long id) {
		Card b = this.get(id);
		return b;
	}
	
	/**
	 * 查找卡行信息
	 * @param id
	 * @param type
	 * @return
	 */
	public List<Object[]> findCardLine(Long id, String type) {
		//项目卡
		if (MrBaseConstant.CardType.ITEM.equals(type)) {
			String hql = "select b.item.id,b.item.name,b.initPrice,b.nowPrice,b.count,b.nowPrice*b.count,b.item.id from BcardLine b left join b.item where b.card.id=? ";
			List<Object[]> list = this.getHT().find(hql, new Object[]{id});
			return list;
		}
		
		//局部卡
		if (MrBaseConstant.CardType.LOCAL.equals(type)) {
			String hql = "select b.item.id,b.item.name,b.item.id from BcardLine b left join b.item where b.card.id=? ";
			List<Object[]> list = this.getHT().find(hql, new Object[]{id});
			return list;
		}
		return new ArrayList();
	}
}
